#!/usr/bin/env python

# --------------------------------------
# projects/python/collatz/TestCollatz.py
# Copyright (C) 2009
# Glenn P. Downing
# --------------------------------------

# To run the tests
#     TestCollatz.py

# To document the tests
#     pydoc -w TestCollatz

import main
import unittest

# ----------
# TestReader
# ----------

class TestReader (object) :
    def __init__ (self, s) :
        self.s = s

    def read (self) :
        return self.s

# ----------
# TestWriter
# ----------

class TestWriter (object) :
    def str (self) :
        return self.s

    def write (self, *a) :
        self.s = str(a[0])
        for w in a[1:] :
            self.s += ' '
            self.s += str(w)
        self.s += '\n'

# -----------
# TestPrimes
# -----------

class TestPrimes (unittest.TestCase) :
    # ----
    # read
    # ----

    def test_read (self) :
        reader = TestReader('1\n')
        main.my_read(reader)
        self.assert_(main.n ==  1)

    # ----
    # my_eval
    # ----

    def test_eval1 (self) :
        main.n =  1
        main.my_eval()
        self.assert_(main.impossible == True)
        
    def test_eval2 (self) :
        main.n =  8
        main.my_eval()
        self.assert_(main.a + main.b + main.c + main.d == 8)
        self.assert_(main.is_prime(main.c) == True)
        self.assert_(main.is_prime(main.d) == True)
        self.assert_(main.impossible == False)
        
    def test_eval3 (self) :
        main.n =  10000000
        main.my_eval()
        self.assert_(main.a + main.b + main.c + main.d == 10000000)
        self.assert_(main.is_prime(main.c) == True)
        self.assert_(main.is_prime(main.d) == True)
        self.assert_(main.impossible == False)
        
    def test_eval4 (self) :
        main.n =  -1
        main.my_eval()
        self.assert_(main.impossible == True)

   


    # -----
    # is Prime
    # ----
    def test_is_prime1 (self):
        self.assert_(main.is_prime(2) == True)
    
    def test_is_prime2 (self):
        self.assert_(main.is_prime(3) == True)
        
    def test_is_prime3 (self):
        self.assert_(main.is_prime(9999991) == True)
        
    def test_is_prime5 (self):
        self.assert_(main.is_prime(4) == False)
        
    def test_is_prime6 (self):
        self.assert_(main.is_prime(1794246) == False)
        
    def test_is_prime7 (self):
        self.assert_(main.is_prime(9999973) == True)
        
    def test_is_prime8 (self):
        self.assert_(main.is_prime(3137) == True)
        
    def test_is_prime9 (self):
        self.assert_(main.is_prime(9999974) == False)
        
    def test_is_prime10 (self):
        self.assert_(main.is_prime(3138) == False)
    
        
        
    # -----
    # print
    # -----

    def test_print (self) :
        main.a =  1
        main.b = 10
        main.c = 20
        main.d = 20
        main.impossible = False
        writer = TestWriter()
        main.my_print(writer)
        self.assert_(writer.str() == '1 10 20 20\n')

if __name__ == "__main__" :
    unittest.main()

       